package com.hnucm.cooklife.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.bean.db.Step;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DishStepAdapter extends RecyclerView.Adapter<DishStepAdapter.StepHolder> {

    private Context mContext;
    private List<Step> mStepList = new ArrayList<>();

    @SuppressLint("NotifyDataSetChanged")
    public void setStepList(List<Step> stepList) {
        mStepList.clear();
        if(stepList != null)
        mStepList.addAll(stepList);
        notifyDataSetChanged();
    }

    public DishStepAdapter(Context context, List<Step> stepList) {
        mContext = context;
        if (stepList != null)
        mStepList.addAll(stepList);
    }

    @NonNull
    @Override
    public StepHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.detail_steps_item, parent, false);
        return new StepHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StepHolder holder, int position) {
        if(mStepList != null){
            Step step = mStepList.get(position);
            holder.mStepNo.setText(
                    Html.fromHtml(String.valueOf(step.snum),Html.FROM_HTML_MODE_COMPACT)
            );
            holder.mStepDesc.setText(
                    Html.fromHtml(step.sdesc,Html.FROM_HTML_MODE_COMPACT)
            );
            Glide.with(mContext)
                    .load(step.spic)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mStepPic);
        }
    }

    @Override
    public int getItemCount() {
        return mStepList == null ? 0 : mStepList.size();
    }

    class StepHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.stepno)
        TextView mStepNo;

        @BindView(R.id.steppic)
        ImageView mStepPic;

        @BindView(R.id.stepdesc)
        TextView mStepDesc;

        public StepHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
